#!/usr/bin/perl

open(INP,$ARGV[0]);

while($senNum=<INP>) {
	print $senNum;
	chomp($senNum);

	my($engSen,@engComps,$numEng,$engWord,@engWords);
	my($hndSen,@hndComps,$numHnd,$hndWord,@hndWords);
	
	$engSen = <INP>;
	@engComps = split(/\s+/,$engSen);
	$numEng = shift(@engComps);
	for($i=0;$i<$numEng;$i++) {
		$engWord = shift(@engComps);
		push(@engWords,$engWord);
	}
	shift(@engComps);
	
#	print $numEng." ## ".join("-",@engComps)."\n";
	for($i=0;$i<$numEng;$i++) {
		if($engComps[$i]!=0) {
			$alignsEnHn[$senNum]->{eval($i).".".eval($engComps[$i]-1)} = 1;
		}
	}

	$hndSen = <INP>;
	@hndComps = split(/\s+/,$hndSen);
	$numHnd = shift(@hndComps);
	for($i=0;$i<$numHnd;$i++) {
		$hndWord = shift(@hndComps);
		push(@hndWords,$hndWord);
	}
	shift(@hndComps);
	
	for($i=0;$i<$numHnd;$i++) {
		if($hndComps[$i]!=0) {
			$alignsHnEn[$senNum]->{eval($hndComps[$i]-1).".".eval($i)} = 1;
		}
	}
	
	foreach $align (keys %{$alignsEnHn[$senNum]}) {
		if(defined $alignsHnEn[$senNum]->{$align}) {
			$alignsInter[$senNum]->{$align} = 1;
		}
		$alignsUnion[$senNum]->{$align} = 1;
	}
	foreach $align (keys %{$alignsHnEn[$senNum]}) {
		$alignsUnion[$senNum]->{$align} = 1;
	}
}
close(INP);

open(OUTENHN,">$ARGV[1]-en-hn.eval");
open(OUTHNEN,">$ARGV[1]-hn-en.eval");
open(OUTINTER,">$ARGV[1]-inter.eval");
open(OUTUNION,">$ARGV[1]-union.eval");
#Print Alignments
for($senNum=1;$senNum<@alignsEnHn;$senNum++) {
	print "Printing $senNum\n";
	foreach $align (sort {$a<=>$b} keys %{$alignsEnHn[$senNum]}) {
		$align=~s/\./\t/g;
		print OUTENHN $senNum."\t".$align."\n";
	}
	foreach $align (sort {$a<=>$b} keys %{$alignsHnEn[$senNum]}) {
		$align=~s/\./\t/g;
		print OUTHNEN $senNum."\t".$align."\n";
	}
	foreach $align (sort {$a<=>$b} keys %{$alignsInter[$senNum]}) {
		$align=~s/\./\t/g;
		print OUTINTER $senNum."\t".$align."\n";
	}
	foreach $align (sort {$a<=>$b} keys %{$alignsUnion[$senNum]}) {
		$align=~s/\./\t/g;
		print OUTUNION $senNum."\t".$align."\n";
	}
}
close(OUTENHN);
close(OUTHNEN);
close(OUTINTER);
close(OUTUNION);

